<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNumber">
        <el-input
          v-model="queryParams.idCardNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['hrmanagement:contractworker:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hrmanagement:contractworker:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hrmanagement:contractworker:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Upload"
          @click="handleExport"
          v-hasPermi="['hrmanagement:contractworker:export']"
        >导出</el-button>
      </el-col>

        <el-upload
                action="http://localhost:9911/contractworker/import"
                accept=".xls,.xlsx"
                :show-file-list="false"
                :on-success="handleImportSuccess"
                :on-error="handleImportError"
                :before-upload="beforeUpload"
        >
            <el-button
                    type="success"
                    plain
                    icon="Download"
                    v-hasPermi="['contractworker:import']"
            >
                导入
            </el-button>
        </el-upload>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractworkerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="出生年月" align="center" prop="birthday" />
<!--      <el-table-column label="等级" align="center" prop="grade">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="sys_grade" :value="scope.row.grade"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="人员分类" align="center" prop="personnelClassification">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="person_type" :value="scope.row.personnelClassification"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="文化程度" align="center" prop="education" />
      <el-table-column label="主职务" align="center" prop="duty" />
      <el-table-column label="头衔" align="center" prop="title" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="民族" align="center" prop="minority" />
      <el-table-column label="入党日期" align="center" prop="joinPartyDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.joinPartyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="政治面貌" align="center" prop="politicalOutlook" />
      <el-table-column label="全日制学历" align="center" prop="fullTimeQualification" />
      <el-table-column label="全日制学位" align="center" prop="fullTimeDegree" />
      <el-table-column label="全日制毕业院校专业" align="center" prop="fullTimeGraduateSchoolSpecialty" />
      <el-table-column label="在职教育学历" align="center" prop="jobEducationQualification" />
      <el-table-column label="在职教育毕业院校专业" align="center" prop="jobGraduateSchoolSpecialty" />
      <el-table-column label="身份证号" align="center" prop="idCardNumber" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="配偶单位" align="center" prop="spousalWorkplace" />
      <el-table-column label="配偶职务" align="center" prop="spousalDuty" />
      <el-table-column label="父母单位" align="center" prop="parentWorkplace" />
      <el-table-column label="父母职务" align="center" prop="parentDuty" />
      <el-table-column label="子女工作单位" align="center" prop="childrenWorkplace" />
      <el-table-column label="子女职务" align="center" prop="childrenDuty" />
      <el-table-column label="正式编制" align="center" prop="formalEstablishment" />
      <el-table-column label="父母姓名" align="center" prop="parentName" />
      <el-table-column label="子女姓名" align="center" prop="childrenName" />
      <el-table-column label="当年公休" align="center" prop="yearPublicHoliday" />
      <el-table-column label="剩余公休" align="center" prop="restPublicHoliday" />
<!--      <el-table-column label="是否删除" align="center" prop="isDeleted" />-->
      <el-table-column label="启/停" align="center" prop="isLocked" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hrmanagement:contractworker:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hrmanagement:contractworker:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同工对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="contractworkerRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出生年月" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入出生年月" />
        </el-form-item>
        <el-form-item label="等级" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择等级">
            <el-option
              v-for="dict in sys_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人员分类" prop="personnelClassification">
          <el-select v-model="form.personnelClassification" placeholder="请选择人员分类">
            <el-option
              v-for="dict in person_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="文化程度" prop="education">-->
<!--          <el-input v-model="form.education" placeholder="请输入文化程度" />-->
<!--        </el-form-item>-->
        <el-form-item label="主职务" prop="duty">
          <el-input v-model="form.duty" placeholder="请输入主职务" />
        </el-form-item>
        <el-form-item label="头衔" prop="title">
          <el-input v-model="form.title" placeholder="请输入头衔" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="民族" prop="minority">
          <el-input v-model="form.minority" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="入党日期" prop="joinPartyDate">
          <el-date-picker clearable
            v-model="form.joinPartyDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择入党日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="政治面貌" prop="politicalOutlook">
          <el-input v-model="form.politicalOutlook" placeholder="请输入政治面貌" />
        </el-form-item>
        <el-form-item label="全日制学历" prop="fullTimeQualification">
          <el-input v-model="form.fullTimeQualification" placeholder="请输入全日制学历" />
        </el-form-item>
        <el-form-item label="全日制学位" prop="fullTimeDegree">
          <el-input v-model="form.fullTimeDegree" placeholder="请输入全日制学位" />
        </el-form-item>
        <el-form-item label="全日制毕业院校专业" prop="fullTimeGraduateSchoolSpecialty">
          <el-input v-model="form.fullTimeGraduateSchoolSpecialty" placeholder="请输入全日制毕业院校专业" />
        </el-form-item>
<!--        <el-form-item label="在职教育学历" prop="jobEducationQualification">-->
<!--          <el-input v-model="form.jobEducationQualification" placeholder="请输入在职教育学历" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="在职教育毕业院校专业" prop="jobGraduateSchoolSpecialty">-->
<!--          <el-input v-model="form.jobGraduateSchoolSpecialty" placeholder="请输入在职教育毕业院校专业" />-->
<!--        </el-form-item>-->
        <el-form-item label="身份证号" prop="idCardNumber">
          <el-input v-model="form.idCardNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="配偶单位" prop="spousalWorkplace">
          <el-input v-model="form.spousalWorkplace" placeholder="请输入配偶单位" />
        </el-form-item>
        <el-form-item label="配偶职务" prop="spousalDuty">
          <el-input v-model="form.spousalDuty" placeholder="请输入配偶职务" />
        </el-form-item>
        <el-form-item label="父母单位" prop="parentWorkplace">
          <el-input v-model="form.parentWorkplace" placeholder="请输入父母单位" />
        </el-form-item>
        <el-form-item label="父母职务" prop="parentDuty">
          <el-input v-model="form.parentDuty" placeholder="请输入父母职务" />
        </el-form-item>
        <el-form-item label="子女工作单位" prop="childrenWorkplace">
          <el-input v-model="form.childrenWorkplace" placeholder="请输入子女工作单位" />
        </el-form-item>
        <el-form-item label="子女职务" prop="childrenDuty">
          <el-input v-model="form.childrenDuty" placeholder="请输入子女职务" />
        </el-form-item>
<!--        <el-form-item label="正式编制" prop="formalEstablishment">-->
<!--          <el-input v-model="form.formalEstablishment" placeholder="请输入正式编制" />-->
<!--        </el-form-item>-->
        <el-form-item label="子女姓名" prop="childrenName">
          <el-input v-model="form.childrenName" placeholder="请输入子女姓名" />
        </el-form-item>
<!--        <el-form-item label="当年公休" prop="yearPublicHoliday">-->
<!--          <el-input v-model="form.yearPublicHoliday" placeholder="请输入当年公休" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="剩余公休" prop="restPublicHoliday">-->
<!--          <el-input v-model="form.restPublicHoliday" placeholder="请输入剩余公休" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否删除" prop="isDeleted">-->
<!--          <el-input v-model="form.isDeleted" placeholder="请输入是否删除" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否禁用" prop="isLocked">-->
<!--          <el-input v-model="form.isLocked" placeholder="请输入是否禁用" />-->
<!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Contractworker">
import { listContractworker, getContractworker, delContractworker, addContractworker, updateContractworker ,importContractworker} from "@/api/hrmanagement/contractworker";

import { ref, getCurrentInstance } from 'vue';



const { proxy } = getCurrentInstance();
const { sys_grade, person_type, sys_user_sex } = proxy.useDict('sys_grade', 'person_type', 'sys_user_sex');

const contractworkerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const url = "http://localhost:9911/contractworker/import"

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    idCardNumber: null,
    phoneNumber: null,
  },
  rules: {
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    gender: [
      { required: true, message: "性别不能为空", trigger: "change" }
    ],
    birthday: [
      { required: true, message: "出生年月不能为空", trigger: "blur" }
    ],
    idCardNumber: [
      { required: true, message: "身份证号不能为空", trigger: "blur" }
    ],
    phoneNumber: [
      { required: true, message: "手机号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询合同工列表 */
function getList() {
  loading.value = true;
  listContractworker(queryParams.value).then(response => {
    contractworkerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    gender: null,
    birthday: null,
    grade: null,
    personnelClassification: null,
    education: null,
    duty: null,
    title: null,
    nativePlace: null,
    minority: null,
    joinPartyDate: null,
    politicalOutlook: null,
    fullTimeQualification: null,
    fullTimeDegree: null,
    fullTimeGraduateSchoolSpecialty: null,
    jobEducationQualification: null,
    jobGraduateSchoolSpecialty: null,
    idCardNumber: null,
    phoneNumber: null,
    spousalWorkplace: null,
    spousalDuty: null,
    parentWorkplace: null,
    parentDuty: null,
    childrenWorkplace: null,
    childrenDuty: null,
    formalEstablishment: null,
    parentName: null,
    childrenName: null,
    yearPublicHoliday: null,
    restPublicHoliday: null,
    isDeleted: null,
    isLocked: null
  };
  proxy.resetForm("contractworkerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加合同工";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getContractworker(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改合同工";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["contractworkerRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateContractworker(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addContractworker(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除合同工编号为"' + _ids + '"的数据项？').then(function() {
    return delContractworker(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('hrmanagement/contractworker/export', {
    ...queryParams.value
  }, `contractworker_${new Date().getTime()}.xlsx`)
}

/** 导入按钮操作 */
function handleImport(file) {
  const formData = new FormData();
  formData.append('file', file.file);
  console.log(file.file);
  console.log(formData);
  importContractworker(formData).then(res => {
      if (res.code === 200) {
          proxy.$modal.msgSuccess(res.msg);
          getList();
      } else {
          proxy.$modal.msgError(res.msg);
      }
  });
}
// 上传前检查文件
function beforeUpload(file) {
    const isExcel = file.type === 'application/vnd.ms-excel' || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
    if (!isExcel) {
        proxy.$message.error('请上传 .xlsx 的Excel 文件');
        return false;
    }
    return true;
}

// 文件导入成功处理
function handleImportSuccess(response) {
    if (response.code === 200) {
        proxy.$message.success('文件导入成功');

        // 如果有错误信息，弹出错误提示框
        if (response.data && response.data.length > 0) {
            proxy.$message.warning('部分数据导入失败：\n' + response.data);
        }
        getList(); // 重新获取数据
    } else {
        proxy.$message.error(response.msg || '文件导入失败');
    }
}

// 文件导入失败处理
function handleImportError(error) {
    proxy.$message.error('文件导入失败，请检查文件格式');
}
/** 查询合同工列表初始化 */
getList();
</script>
